DP G
DP_G
G
The problem of finding the longest path in a graph
1TLE
code:python
def solve(N, M, edges):
def get_longest(start):
next_edges = edges.get(start)
if not next_edges:
ret = 0
else:
ret = max(get_longest(v) for v in edgesstart) + 1 return ret
return max(get_longest(v) for v in edges)
Python 428 ms AC
code:python
def solve(N, M, edges):
for i in range(N + 1):
def get_longest(start):
for v in next:
longestv = get_longest(v) ret = max(longestv for v in next) + 1 return ret
for i in range(N + 1):
longesti = get_longest(i) return max(longestv for v in edges) Cython
---
This page is auto-translated from /nishio/DP G. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.